package com.yfuxdeer.youchat.dao.impl;

import com.yfuxdeer.youchat.dao.MomentDao;
import com.yfuxdeer.youchat.pojo.Moment;
import com.yfuxdeer.youchat.utils.Store;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MomentDaoImpl implements MomentDao {
    private JdbcTemplate template = new JdbcTemplate(Store.getDataSource());

    @Override
    public List<Moment> findAll(String hostId) {
        return template.query("select * from y_moment where host_id=? order by time desc",
                new BeanPropertyRowMapper<>(Moment.class),
                new Object[]{hostId});
    }

    @Override
    public void insert(Moment moment) {
        template.update("insert into y_moment(host_id,content) values(?,?)", new Object[]{moment.getHost_id(), moment.getContent()});
    }

    @Override
    public Moment findLastById(String username) {
        List<Moment> momentList = template.query("select * from y_moment where host_id=? order by time desc limit 1",
                new BeanPropertyRowMapper<>(Moment.class), new Object[]{username});
        if(momentList.isEmpty())return null;
        return momentList.get(0);
    }
}
